
#ifndef _IM_AVBUF_H
#define _IM_AVBUF_H



#define IM_AVBUF_PKTTYPE_VIDEO 0
#define IM_AVBUF_PKTTYPE_AUDIO 1
#define IM_AVBUF_PKTTYPE_DATA 2
#define RTP_VERSION 2

typedef struct im_packet_
  {

    int size;
    int payload_type;
    unsigned short seq;
    unsigned int ssrc;
    unsigned int timestamp;
    char *data;

  }
im_packet_t;


typedef struct im_rtppacket_
  {

    int size;
    int payload_type;
    int seq;
    unsigned int ssrc;
    unsigned int timestamp;
    char data[1024];

  }
im_rtppacket_t;
/*
 * NOTE: it is limited thread safe for avbuf functions:
 * 		all avbuf function MUST be called after 'im_avbuf_init' done and before 'im_avbuf_exit' begin.
 * 		all avbuf function use the 'avbuf_id' as arg
 *      MUST be called after 'new' done and before 'delete(the avbuf_id)' begin
 */

enum CALL
{
  caller=0,
  callee,
};

void im_avbuf_init(int max_productor);
/* user MUST free all avbuf before exit, or memory leak will be happened */
void im_avbuf_exit(void);

/* return: a avbuf id */
int im_avbuf_new(int sessionch,int max_size, void (* free_cb)(void *));
int im_avbuf_delete(int avbuf_id);

int im_avbuf_get_size(int avbuf_id, int type);
//int64_t im_avbuf_get_duration(int avbuf_id, int type);

void im_avbuf_dump(int avbuf_id);


int im_avbuf_pkt_put(int avbuf_id, im_packet_t *pkt,int client);

im_packet_t *im_avbuf_pkt_get(int avbuf_id, int client);

im_packet_t *im_avbuf_pkt_latest(int avbuf_id,int client);
im_packet_t *im_avbuf_pkt_oldest(int avbuf_id,int client);

/* NOTE: the 'pkt' used by 'pkt_next' and 'pkt_release' MUST be get from avbuf. */
im_packet_t *im_avbuf_pkt_next(im_packet_t *pkt,int client);
void im_avbuf_pkt_release(im_packet_t *pkt);

void im_avbuf_pkt_clear(int avbuf_id);
int im_rtpdata_set(char *data,int datalen,int sessionch,int calldir);
int im_avbuf_pkt_getrtpdata(int avbuf_id,im_rtppacket_t *rtppket,int client);

int im_avbuf_getmaxbuf();
int im_avbuf_cmprtpsession(int ch,int sessionch);
int im_avbuf_checkRtpch(int ch);

//unsigned __int64 decode_be64(const u_char *p);

//unsigned int decode_be32(const u_char *p);

#endif

